cssmatcher: Simplify code
authorBenjamin Otte <otte@redhat.com>
Thu, 28 May 2015 15:06:53 +0000 (17:06 +0200)
committerBenjamin Otte <otte@redhat.com>
Thu, 28 May 2015 15:07:25 +0000 (17:07 +0200)
Instead of copy/paste, pass the only difference as an argument

The code still doesn't work right, but at least it's easier to fix now.

gtk/gtkcssmatcher.c

index b2b5c75a53d64f55ee282c35e20baaf5bc3b1aa1..c68f650915a96c57acb2a67e18357e222f0be9cf 100644 (file)
@@ -352,6 +352,7 @@ gtk_css_matcher_node_has_region (const GtkCssMatcher *matcher,
 
 static gboolean
 gtk_css_matcher_node_nth_child (GtkCssNode *node,
+                                GtkCssNode *(* prev_node_func) (GtkCssNode *),
                                 int         a,
                                 int         b)
 {
@@ -360,7 +361,7 @@ gtk_css_matcher_node_nth_child (GtkCssNode *node,
       if (node == NULL)
         return FALSE;
 
-      node = get_previous_visible_sibling (node);
+      node = prev_node_func (node);
     }
 
   if (a == 0)
@@ -372,35 +373,7 @@ gtk_css_matcher_node_nth_child (GtkCssNode *node,
   while (node)
     {
       b++;
-      node = get_previous_visible_sibling (node);
-    }
-
-  return b % a == 0;
-}
-
-static gboolean
-gtk_css_matcher_node_nth_last_child (GtkCssNode *node,
-                                     int         a,
-                                     int         b)
-{
-  while (b-- > 0)
-    {
-      if (node == NULL)
-        return FALSE;
-
-      node = get_next_visible_sibling (node);
-    }
-
-  if (a == 0)
-    return node == NULL;
-  else if (a == 1)
-    return TRUE;
-
-  b = 0;
-  while (node)
-    {
-      b++;
-      node = get_next_visible_sibling (node);
+      node = prev_node_func (node);
     }
 
   return b % a == 0;
@@ -412,10 +385,10 @@ gtk_css_matcher_node_has_position (const GtkCssMatcher *matcher,
                                    int                  a,
                                    int                  b)
 {
-  if (forward)
-    return gtk_css_matcher_node_nth_child (matcher->node.node, a, b);
-  else
-    return gtk_css_matcher_node_nth_last_child (matcher->node.node, a, b);
+  return gtk_css_matcher_node_nth_child (matcher->node.node,
+                                         forward ? get_previous_visible_sibling 
+                                                 : get_next_visible_sibling,
+                                         a, b);
 }
 
 static const GtkCssMatcherClass GTK_CSS_MATCHER_NODE = {